// localLifePackage/pages/payMoney/payMoney.js
var app = getApp();

var common = require("../../../common/common.js").common;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        money: "",
        remark: "",
        business_id: "",
        disabled: false
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        if (options.scene) {
            var scene = decodeURIComponent(options.scene);
            scene = scene.split(":");
            scene.map(item => {
                var key = item.split("=");
                if (key[0] == "m") {
                    this.setData({
                        money: key[1],
                        disabled: true
                    });
                } else if (key[0] == "b") {
                    this.setData({
                        business_id: key[1]
                    });
                }
            });
        } else {
            this.setData({
                business_id: options.id || ""
            });
        }
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    recordValue: function(e) {
        var key = e.currentTarget.dataset.name;
        this.setData({
            [key]: e.detail.value
        });
    },
    payOrder: function() {
        let that = this;
        if (!that.data.money) {
            wx.showToast({
                icon: "none",
                title: "请输入付款金额"
            });
            return false;
        }
        common.request({
            endpoint: "Localbusiness/order/pay",
            method: "post",
            data: {
                business_id: this.data.business_id,
                sale_price: this.data.money,
                market_price: this.data.money,
                remark: that.data.remark,
                pay_to: "face"
            },
            success: res => {
                console.log(res);
                that.payWay(res, res.order_no, res.pay.order_id);
            },
            fail(err) {
                wx.showModal({
                    title: "提示",
                    content: err.message
                });
            }
        });
    },
    /**
   * 
   * 微信支付
   * 
   */
    payWay(res, order_no, order_id) {
        wx.requestPayment({
            timeStamp: res.pay.timeStamp,
            nonceStr: res.pay.nonceStr,
            package: res.pay.package,
            signType: res.pay.signType,
            paySign: res.pay.paySign,
            success: function(res) {
                console.log(res);
                wx.showToast({
                    title: "支付成功",
                    icon: "success",
                    mask: true,
                    duration: 3e3,
                    success: function(res) {
                        wx.showToast({
                            title: "购买成功！",
                            icon: "success",
                            success: function() {
                                setTimeout(function() {
                                    wx.reLaunch({
                                        url: `/localLifePackage/pages/reward/reward?id=${order_id}`
                                    });
                                }, 500);
                            }
                        });
                    }
                });
            },
            fail: function(res) {
                wx.navigateTo({
                    url: "/localLifePackage/pages/order/order"
                });
            },
            complete: function(res) {
                wx.reLaunch({
                    url: "/localLifePackage/pages/order/order"
                });
            }
        });
    }
});